package net.hvacapp.cmvrfforandroid.ui.fragment;


import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.URLSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.mirror.android.common.util.StringUtils;

import net.hvacapp.cmvrfforandroid.R;
import net.hvacapp.cmvrfforandroid.constant.Constant;
import net.hvacapp.cmvrfforandroid.http.BasicHttpClient;
import net.hvacapp.cmvrfforandroid.http.CommonResponse;
import net.hvacapp.cmvrfforandroid.http.HttpCallbackHandle;
import net.hvacapp.cmvrfforandroid.http.HttpConstant;
import net.hvacapp.cmvrfforandroid.manager.AccountManager;
import net.hvacapp.cmvrfforandroid.manager.ProjectManager;
import net.hvacapp.cmvrfforandroid.model.GetUnreadMessageResp;
import net.hvacapp.cmvrfforandroid.ui.BaseActivity;
import net.hvacapp.cmvrfforandroid.ui.EventBus.NewMessageRefreshEvent;
import net.hvacapp.cmvrfforandroid.ui.EventBus.ProjectListRefreshEvent;
import net.hvacapp.cmvrfforandroid.ui.WebviewActivity;
import net.hvacapp.cmvrfforandroid.ui.mine.AccountManagerActivity;
import net.hvacapp.cmvrfforandroid.ui.mine.NoticeManagerActivity;
import net.hvacapp.cmvrfforandroid.ui.mine.ProjectApproveActivity;
import net.hvacapp.cmvrfforandroid.ui.mine.StatisticsListActivity;
import net.hvacapp.cmvrfforandroid.ui.mine.SuggestionManagerActivity;
import net.hvacapp.cmvrfforandroid.ui.mine.UserApproveActivity;
import net.hvacapp.cmvrfforandroid.ui.more.AboutActivity_;
import net.hvacapp.cmvrfforandroid.ui.project.model.GetProjectInfoDataResp;
import net.hvacapp.cmvrfforandroid.utils.AnnotationUtil;
import net.hvacapp.cmvrfforandroid.widget.CMActionBar;
import net.hvacapp.cmvrfforandroid.widget.ImageTitleCellView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * A simple {@link Fragment} subclass.
 */
@EFragment(R.layout.fragment_mine)
public class MineFragment extends BaseFragment {

    @ViewById(R.id.mine_account)
    ImageTitleCellView mAccountCv;
    @ViewById(R.id.mine_account_approve)
    ImageTitleCellView mAccountApproveCv;
    @ViewById(R.id.mine_project_approve)
    ImageTitleCellView mProjectApproveCv;
    @ViewById(R.id.mine_statistics)
    ImageTitleCellView mStatisticeCv;
    @ViewById(R.id.mine_suggestion)
    ImageTitleCellView mSuggestionCv;
    @ViewById(R.id.mine_notice)
    ImageTitleCellView mNoticeCv;
    @ViewById(R.id.mine_function)
    ImageTitleCellView mFunctionCv;
    @ViewById(R.id.mine_logout)
    ImageTitleCellView mLogoutCv;

    @ViewById(R.id.tv_midea_site)
    TextView mMideaSiteTv;
    @ViewById(R.id.tv_cm_site)
    TextView mCmSiteTv;
    @ViewById(R.id.tv_version)
    TextView mVersionTv;


    public MineFragment() {
        // Required empty public constructor
    }


    @AfterViews
    public void init(){
//        initActionBar();
        EventBus.getDefault().register(this);

        mAccountCv.setTitleString("账号管理");
        mAccountCv.setIconRes(R.drawable.mine_account);
        mAccountApproveCv.setTitleString("用户审核");
        mAccountApproveCv.setIconRes(R.drawable.mine_user_approve);
        mProjectApproveCv.setTitleString("项目审核");
        mProjectApproveCv.setIconRes(R.drawable.mine_project_approve);
        mStatisticeCv.setTitleString("数据统计");
        mStatisticeCv.setIconRes(R.drawable.mine_statistics_data);
        mNoticeCv.setTitleString("公告管理");
        mNoticeCv.setIconRes(R.drawable.mine_notice_manager);
        mSuggestionCv.setTitleString("意见反馈");
        mSuggestionCv.setIconRes(R.drawable.mine_suggestion);
        mFunctionCv.setTitleString("功能介绍");
        mFunctionCv.setIconRes(R.drawable.mine_function);
        mLogoutCv.setTitleString("注销");
        mLogoutCv.setIconRes(R.drawable.mine_loginout);

        //控制显示隐藏项目
        String role = AccountManager.getInstance().getUserInfo().getRole();
        if (StringUtils.isEquals(role, Constant.UserTypeSalePersonal)) {
            mAccountApproveCv.setVisibility(View.GONE);
            mProjectApproveCv.setVisibility(View.GONE);
            mStatisticeCv.setVisibility(View.GONE);
            mNoticeCv.setVisibility(View.GONE);
        }

        mMideaSiteTv.setMovementMethod(LinkMovementMethod.getInstance());
        SpannableString sp = new SpannableString("美的官网");
        sp.setSpan(new URLSpan("http://www.midea.com/"), 0, sp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mMideaSiteTv.setText(sp);

        mCmSiteTv.setMovementMethod(LinkMovementMethod.getInstance());
        SpannableString sp1 = new SpannableString("优能易创官网");
        sp1.setSpan(new URLSpan("http://www.hvacapp.com.cn"), 0, sp1.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        mCmSiteTv.setText(sp1);

        PackageManager manager;
        PackageInfo info = null;
        manager = getActivity().getPackageManager();
        try {
            info = manager.getPackageInfo(getActivity().getPackageName(), 0);
            String version = "当前版本号：V" + info.versionName;
            mVersionTv.setText(version);

        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }


        loadUnreadSuggestionCount();
        initEvent();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

        if (!hidden){
//            initActionBar();

        }
    }

    private void initEvent() {
        mLogoutCv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //注销账户
                AccountManager.getInstance().logout();
                ProjectManager.getInstance().logout();
                getActivity().finish();
            }
        });
        mAccountApproveCv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AnnotationUtil.get(UserApproveActivity.class)));
            }
        });
        mProjectApproveCv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AnnotationUtil.get(ProjectApproveActivity.class)));
            }
        });
        mStatisticeCv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AnnotationUtil.get(StatisticsListActivity.class)));
            }
        });
        mNoticeCv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AnnotationUtil.get(NoticeManagerActivity.class)));
            }
        });
        mSuggestionCv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AnnotationUtil.get(SuggestionManagerActivity.class)));
            }
        });
        mFunctionCv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AnnotationUtil.get(WebviewActivity.class));
                intent.putExtra("title", "功能介绍");
                intent.putExtra("url", HttpConstant.HTTP_FUNCTIONARY_INTRODUCE);
                startActivity(intent);
            }
        });
        mAccountCv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), AnnotationUtil.get(AccountManagerActivity.class)));
            }
        });
    }

    private void loadUnreadSuggestionCount() {

        BasicHttpClient.httpGetAsync(HttpConstant.HTTP_GET_UNREAD_SUGGESTIONS, AccountManager.getInstance().getToken(), new HttpCallbackHandle<GetUnreadMessageResp>() {
            @Override
            public void onSuccess(GetUnreadMessageResp obj) {
                // 成功
                if (obj != null && obj.getGetUnReadSuggestionResult() > 0) {
                    mSuggestionCv.setIconRes(R.drawable.mine_suggestionwithred);
                } else {
                    mSuggestionCv.setIconRes(R.drawable.mine_suggestion);
                }
            }

            @Override
            public void onFailure(String response) {

            }

        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    /**
     * 联系人任务未处理
     *
     * @param event
     */
    public void onEvent(NewMessageRefreshEvent event) {
        loadUnreadSuggestionCount();
    }


}
